/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Hua YY
 * Date: 2025-01-06
 * Time: 13:06
 */
class Solution4 {
    public String addBinary(String a, String b) {

        int len1 = a.length() , len2 = b.length();
        int cur1 = len1-1 , cur2 = len2-1;
        int tmp = 0;
        StringBuilder builder = new StringBuilder();
        while(cur1 >= 0 || cur2 >= 0){
            if(cur1 >= 0){
                tmp += a.charAt(cur1) == '0' ? 0 : 1;
                cur1--;
            }
            if(cur2 >= 0){
                tmp += b.charAt(cur2) == '0' ? 0 : 1;
                cur2--;
            }
            if(tmp == 0){
                builder.append('0');
            }else if(tmp == 1){
                builder.append('1');
                tmp = 0;
            }else if(tmp == 2){
                builder.append('0');
                tmp = 1;
            }else if(tmp == 3){  //穷举法（包含进位的数字）
                builder.append('1');
                tmp = 1;
            }
        }
        if(tmp == 1){
            builder.append('1');//处理最前端还有进位的情况
        }
        return builder.reverse().toString();
    }
}
public class Test4 {
}
